<?php namespace Model;

    class orderModel {
        private $DB;

        public function __construct() {
            $this->DB = mysqli_connect('kvd_mysql', 'root', 'root', 'kvd');
        }

        public function addOrder($user_id, $event_id) {
            $user_id = (integer) $user_id;
            $event_id = (integer) $event_id;
            $token = md5($user_id.$event_id);
            $data = date("Y.m.d");

            $sql = "select * from orders where user_id = $user_id and event_id = $event_id";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response !== null){
                return [
                    "response" => "Mismatch order",
                    "status" => 0
                ];
            }

            $sql = "INSERT INTO orders (user_id ,event_id, status, token, created, updated)
                            VALUES
                            ($user_id , $event_id, 0,  '$token', '$data', '$data')";
            $this->DB->query($sql);

            $sql = "select * from orders where user_id = $user_id and event_id = $event_id";
            $response = $this->DB->query($sql)->fetch_assoc();

            if ($response !== null) {
                return [
                    "response" => $response,
                    "status" => "1",
                ];
            } else {
                return [
                    "status" => "0"
                ];
            }
        }

        public function getAllOrders() {
            $sql = "select * from kvd.events where 1;";
            $sql = $this->DB->real_escape_string($sql);
            $response = $this->DB->query($sql)->fetch_all(MYSQLI_ASSOC);
            if ($response != null) {
                return [
                    "response" => $response,
                    "status" => 1
                ];
            } else {
                return [
                    "status" => 0
                ];
            }
        }

        public function deleteOrder($user_id, $event_id) {
            $sql = "select * from kvd.orders where user_id = $user_id and event_id = $event_id";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response == null){
                return [
                    "status" => 0
                ];
            }
            $sql = "delete from orders where user_id = $user_id and event_id = $event_id";
            $response = $this->DB->query($sql);
            $sql = "select * from kvd.orders where user_id = $user_id and event_id = $event_id";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response == null){
                return [
                    "status" => 1
                ];
            } else {
                return [
                    "status" => 0
                ];
            }
        }

        public function getOrdertById($id = null) {
            if (!is_integer($id)) {
                return [
                    "status" => 0
                ];
            }
            $sql = "select * from events where id = '$id'";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response !== null) {
                return [
                    "response" => $response,
                    "status" => 1
                ];
            } else {
                return [
                    "status" => 0
                ];
            }
        }
    }